<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 12.5.&nbsp; Updating Data</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-12-SECT-4.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-12-SECT-6.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><TD valign="top"><a name="learnphpmysql-CHP-12-SECT-5"></a>
<h3 id="title-IDA5SMAJ" class="docSection1Title">12.5. Updating Data</h3>
<a name="IDX-CHP-12-0599"></a> 

<p class="docText">Since you've been inputting table data, you can change existing records. You'll probably only do this if there are errors in your data, or in the instance that user data has changed and needs to be updated in the database. Updates are handled as shown in <a class="docLink" href="#learnphpmysql-CHP-12-EX-8">Example 12-8</a>.</p>
<a name="learnphpmysql-CHP-12-EX-8"></a><h5 id="title-IDARTMAJ" class="docExampleTitle">Example 12-8. Updating a field</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
require_once('db_login.php');
require_once('DB.php');
$connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
if (DB::isError($connection)){
die ("Could not connect to the database: &lt;br /&gt;". DB::errorMessage($connection));
}
<span class="docEmphStrong">$query = "UPDATE `books` SET `pages`=558 WHERE `title_id`=2";</span>
$result = $connection-&gt;query($query);
if (DB::isError($result)){
die("Could not query the database: &lt;br /&gt;".$query." ".DB::errorMessage($result));
}
echo "Updated successfully!";
$connection-&gt;disconnect();
?&gt;
</pre><br>

</td></tr></table></p>
<p class="docText">If you have multiple columns to edit in one record at a single time, you'd separate the code with a comma (,). Again, you could have used a dynamic value such as a form input in the <tt>WHERE</tt> clause. If you use a <tt>WHERE</tt> clause, you'd have to specify what rows the update affects; otherwise, the change applies to every row.</P>
<p class="docText">Updates and deletions are two of most important reasons to use a primary key. The primary key number, which never should change, can be a point of reference in the <tt>WHERE</tt> clause. In <a class="docLink" href="#learnphpmysql-CHP-12-FIG-9">Figure 12-9</a>, you see the new value in the <tt>books</tt> table from the <tt>mysql</tt> client.</p>
<a name="learnphpmysql-CHP-12-FIG-9"></a><p><center>
<H5 class="docFigureTitle">Figure 12-9. The new page count of 558 appears in the table</H5>
<img border="0" alt="" width="549" height="135" SRC="images/learnphpmysql_1209.jpg">
</center></p><BR>
<p class="docText">Since updates and deletions are so important, you'll perform a delete operation. As a precaution against accidentally updating too many rows, apply a <tt>limit</tt> clause with your update.</p>
<p><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><tr><TD><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><tr><TD width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></td><td valign="top">
<p class="docText">Never update a primary key column. This value should never change. If you change a primary key in one table, it could affect the data<a name="IDX-CHP-12-0600"></a> 
 in another table.</P>
</TD></tr></table></TD></tr></table></p><BR>

<a href="11011536.html"><img src="images/pixel.jpg" alt="" width="1" height="1" border="0"></a></TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-12-SECT-4.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-12-SECT-6.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
</body></html>
